package blitting {
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import views.Canvas;
	
	/**
	 * @author Charlie MacIsaac
	 */
	
	public class SpriteSheetAnimation extends SpriteSheetDisplayObject {
		// PROPERTIES =============================================================================
		private var _totalFrames : uint;
		private var _playing : Boolean;
		private var _loop : Boolean;
		
		// GETTERS + SETTERS ======================================================================
		public function get frame() : uint { return _frame; }
		public function get totalFrames() : uint { return _totalFrames; }
		
		public function set loop(value : Boolean) : void { _loop = value; }
		
		
		// CONSTRUCTOR ============================================================================
		public function SpriteSheetAnimation(renderer : SpriteSheetRenderer, loop : Boolean ) {
			super(renderer);
			_totalFrames = _renderer.numberOfTiles;
			_playing = false;
			_loop = loop;
		}
		
		// PUBLIC =================================================================================
		public function play(fromFrame : uint = uint.MAX_VALUE) : void {
			if (fromFrame != uint.MAX_VALUE) {
				frame = fromFrame;
			}
			_playing = true;
			
		}
		
		override public function render() : void {
			if (_canvas) _renderer.render(_frame, _canvas.bitmapData, new Point(x, y));
			if (_playing) {
				var nextFrame : uint = _frame + 1;
				if (nextFrame >= _totalFrames) {
					if (_loop) frame = 0;
				}
				else {
					frame = nextFrame;
				}
			}
		}
		
		// PRIVATE ================================================================================
		// LISTENERS ==============================================================================
	}
}